package com.sh.rbac.controller;

import com.sh.rbac.domain.dto.user.LoginDTO;
import com.sh.rbac.domain.entity.User;
import com.sh.rbac.result.Result;
import com.sh.rbac.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 登录控制器
 *
 * @author lucky
 * @since 2024-04-07
 */

@Tag(name = "登录管理", description = "登录相关操作")
@RequestMapping("sys/user")
@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @Operation(summary = "登录", description = "用户登录")
    @ApiResponse(responseCode = "200", description = "登录成功")
    @PostMapping("login")
    public Result<Map<String, Object>> login(@Valid @RequestBody LoginDTO loginDTO) {
        return Result.success(userService.login(loginDTO));
    }

    @Operation(summary = "获取用户信息", description = "获取用户信息")
    @ApiResponse(responseCode = "200", description = "获取成功")
    @GetMapping("info")
    public Result<User> getInfo(String token) {
        return Result.success(userService.getInfo(token));
    }

    @Operation(summary = "退出登录", description = "用户退出登录")
    @ApiResponse(responseCode = "200", description = "成功退出登录")
    @PostMapping("logout")
    public Result<Boolean> logout() {
        return userService.logout() ? Result.success() : Result.failure();
    }


    @Operation(summary = "获取验证码", description = "获取图形验证码")
    @ApiResponse(responseCode = "200", description = "获取成功")
    @GetMapping("captcha")
    public Result<String> captcha(String randomStr) {
        return Result.success(userService.captcha(randomStr));
    }
}
